package com.travel.server.manager.impl.command.adversiting;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.advertising.QueryAdvertisingPositionListRequest;
import com.travel.server.manager.dto.response.advertising.QueryAdvertisingPositionListResponse;
import com.travel.server.manager.dto.response.advertising.QueryAdvertisingPositionListResponse.PositionItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.impl.dal.PositionDal;
import com.travel.server.manager.model.AdvertisingPositionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 14:42
 * @Desc:
 */
@Component
@Slf4j
public class QueryAdvertisingPositionListCommand extends AbstractCommand<QueryAdvertisingPositionListRequest, QueryAdvertisingPositionListResponse> {

    @Autowired
    private PositionDal positionDal;

    @Override
    protected void checkInput(QueryAdvertisingPositionListRequest request) {
        checkPageInput(request);
    }

    @Override
    public QueryAdvertisingPositionListResponse handle(QueryAdvertisingPositionListRequest request) {
        checkInput(request);

        PageDto<AdvertisingPositionInfo> pageDto = positionDal.selectByPage(request.getOffset(), request.getLimit());
        List<PositionItem> positionList = new ArrayList<>();
        QueryAdvertisingPositionListResponse response = new QueryAdvertisingPositionListResponse();
        response.setTotalNum(pageDto.getTotal());
        List<AdvertisingPositionInfo> list = pageDto.getList();
        if(!CollectionUtil.isEmpty(list)){
            for(AdvertisingPositionInfo info : list){
                PositionItem item = new PositionItem();
                item.setDesc(info.getPositionDescribe());
                item.setHeight(info.getPositionHeight());
                item.setWeight(info.getPositionWeight());
                item.setId(info.getId());
                item.setName(info.getPositionName());
                positionList.add(item);
            }
        }
        response.setPositionList(positionList);

        return response;
    }
}
